﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace ImgCore
{
    public class Img
    {
        #region 实例成员

        protected Image sourceImage = null;
        protected string sourcePath = null;

        private Img()
        {
        }

        public Img(string file)
        {
            if (File.Exists(file))
            {
                sourcePath = file;
                sourceImage = Image.FromFile(file);
            }
            else
            {
                throw new IOException(string.Format("文件 '{0}' 不存在。", file));
            }
        }

        public Img(Image image)
        {
            sourceImage = image;
        }

        ~ Img()
        {
            if (sourceImage != null)
            {
                sourceImage.Dispose();
                sourceImage = null;
            }
        }

        public void SetPath(string path)
        {
            if (path != null)
            {
                sourcePath = path;
            }
        }

        public void Save()
        {
            if (sourcePath == null)
            {
                throw new Exception("要保存的图像必须存在一个确定的路径。");
            }
            sourceImage.Save(sourcePath);
        }

        public void Replace()
        {
            if (sourcePath == null)
            {
                throw new Exception("要保存的图像必须存在一个确定的路径。");
            }

            Image savedImage = new Bitmap(sourceImage);
            savedImage.Save(sourcePath);
            sourceImage.Dispose();
            sourceImage = savedImage;
        }

        public Img Copy(Rectangle zone)
        {
            Img ret = new Img();
            ret.sourceImage = new Bitmap(zone.Width, zone.Height);

            using (Graphics pickedGraphics = Graphics.FromImage(ret.sourceImage))
            {
                pickedGraphics.DrawImage(sourceImage, new Rectangle(0, 0, ret.sourceImage.Width, ret.sourceImage.Height), zone, GraphicsUnit.Pixel);
                pickedGraphics.Flush();
            }

            return ret;
        }

        public void DrawTo(Graphics targetGraphics, Point location)
        {
            targetGraphics.DrawImage(sourceImage, new Rectangle(location.X, location.Y, sourceImage.Width, sourceImage.Height), new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), GraphicsUnit.Pixel);
            targetGraphics.Flush();
        }

        public int Width
        {
            get
            {
                if (sourceImage != null)
                {
                    return sourceImage.Width;
                }
                else
                {
                    throw new Exception("无法从一个未初始化的图形对象获取宽度。");
                }
            }
        }

        public Image Image
        {
            get
            {
                if (sourceImage != null)
                {
                    return sourceImage;
                }
                else
                {
                    throw new Exception("无法返回一个未初始化的图形对象。");
                }
            }
        }

        public int Height
        {
            get
            {
                if (sourceImage != null)
                {
                    return sourceImage.Height;
                }
                else
                {
                    throw new Exception("无法从一个未初始化的图形对象获取高度。");
                }
            }
        }

        public void Split(int unitWidth, int unitHeight)
        {
            if (sourcePath == null)
            {
                throw new Exception("要切分的图像必须存在于一个确定的路径。");
            }

            string destDir = Path.GetDirectoryName(sourcePath);
            string filename = Path.GetFileNameWithoutExtension(sourcePath);
            string fileExt = Path.GetExtension(sourcePath);
            destDir = Path.Combine(destDir, filename);

            if (!Directory.Exists(destDir))
            {
                Directory.CreateDirectory(destDir);
            }

            for (int yPos = 0; yPos < sourceImage.Height; yPos = yPos + unitHeight)
            {
                int currentHeight = unitHeight;
                string currentRow = (yPos / unitHeight + 1).ToString("#0");

                if (yPos > sourceImage.Height - unitHeight)
                {
                    currentHeight = sourceImage.Height - yPos;
                }

                for (int xPos = 0; xPos < sourceImage.Width; xPos = xPos + unitWidth)
                {
                    int currentWidth = unitWidth;
                    string currentCol = (xPos / unitWidth + 1).ToString("#0");

                    if (xPos > sourceImage.Width - unitWidth)
                    {
                        currentWidth = sourceImage.Width - xPos;
                    }

                    Rectangle zone = new Rectangle(xPos, yPos, currentWidth, currentHeight);
                    Img pickedImg = this.Copy(zone);
                    pickedImg.sourcePath = Path.Combine(destDir, string.Format("{0}_{1}_{2}{3}", filename,currentRow, currentCol, fileExt));
                    pickedImg.Save();
                    pickedImg = null;
                }
            }
            
        }
        #endregion
    }
}
